package com.monboo.utils
{
	import com.gs.TweenLite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class DragRoll extends Sprite
	{
		private var _stage:Stage;
		private var _target:Sprite;
		private var _rollMc:Sprite;
		private var _dragLine:Sprite;
		
		private var _lenght:Number;
		private var _totalPage:Number;
		private var _lastPagePicNum:Number;
		
		private var numChild:uint;
		private var pageNum:uint;
		private var totalPage:uint;
		private var aix:String;
		
		private var childWidth:uint;
		private var childHeight:uint;
		private var childMargin:uint;
		
		private var allPageLenght:Number;
		private var toDragLength:Number;
		private var perPageLenght:Number;
		
		private var rect:Rectangle;
		private var scrubbing:Boolean = false;
		private var num:Number= 0;
		private var oldNum:Number = 0;
		private var dragPage:Number = 0;
		private var oldDragPage:Number = 2;
		
		public function DragRoll(target:Sprite,rollMc:Sprite,dragLine:Sprite,$vars:Object,$aix:String="x")
		{
			_target = target;
			_rollMc = rollMc;
			_dragLine = dragLine;
			aix = $aix;
			
			numChild = $vars["totalNum"];
			pageNum = $vars["pageNum"];
			
			childMargin = $vars["childMargin"];
			childWidth = $vars["childWidth"];
			childHeight = $vars["childHeight"];
			
			_dragLine.x = 0;
			_dragLine.y = 0;
			addChild(_dragLine);
			
			
			_target.x = 0;
			_target.y = 0;
			addChild(_target);
			
			if(aix == "x"){
				_dragLine.width = pageNum*(childWidth+childMargin)-childMargin;
				_lenght = pageNum*(childWidth+childMargin)-childMargin-target.width;
				rect = new Rectangle(0,0,_lenght,0);
				perPageLenght = pageNum*(childWidth+childMargin);
				toDragLength = _rollMc.width+childMargin-pageNum*(childWidth+childMargin);
				allPageLenght = perPageLenght*(_totalPage+1);
			}else{
				_dragLine.height = pageNum*(childHeight+childMargin)-childMargin;
				_lenght = pageNum*(childHeight+childMargin)-childMargin-target.height;
				rect = new Rectangle(0,0,0,_lenght);
				perPageLenght = pageNum*(childHeight+childMargin);
				toDragLength = _rollMc.height+childMargin-pageNum*(childHeight+childMargin);
				allPageLenght = perPageLenght*(_totalPage+1);
			}
			
			
			_totalPage = Math.ceil(numChild/pageNum)-1;
			_lastPagePicNum = lastPageNum(_totalPage);
			
			addEventListener(Event.ADDED_TO_STAGE,addToStageFunc);	
		}
		private function addToStageFunc(e:Event):void{
			_stage = this.parent.stage;
				
			_target.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		private function onMouseDown(event:MouseEvent):void {
			scrubbing = true;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_target.startDrag(false, rect);
		}
		private function onMouseUp(event:MouseEvent):void {
			if(scrubbing){
				scrubbing = false;
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				_target.stopDrag( );
			}
		}
		private function onEnterFrame(e:Event):void {
			if(aix == "x"){
				if(oldNum != int(_target.x)){
					num = int(_target.x);
					oldNum = num;
					
					_rollMc.x = -toDragLength*(num/_lenght);		
					dragPage = getObjNum(-(_rollMc.x),allPageLenght,_totalPage+1);
					
					if(oldDragPage != dragPage){
						dispatchEvent(new Event("loadPage"));
						oldDragPage = dragPage;
					}		
				}	
			}else{
				if(oldNum != int(_target.y)){
					num = int(_target.y);
					oldNum = num;
					
					_rollMc.y = -toDragLength*(num/_lenght);		
					dragPage = getObjNum(-(_rollMc.y),allPageLenght,_totalPage+1);
					
					if(oldDragPage != dragPage){
						dispatchEvent(new Event("loadPage"));
						oldDragPage = dragPage;
					}
				}	
			}
				
		}
		
		public function pickThumbBar(n:Number):void{
			if(aix == "x"){
				if(n<_target.x){
					num = n;
				}else if(n>_target.x){
					num = n-_target.width;
				}
			}else{
				if(n<_target.y){
					num = n;
				}else if(n>_target.y){
					num = n-_target.width;
				}
			}
			
			TweenLite.killTweensOf(_target);
			TweenLite.killTweensOf(_rollMc);
			var rollNum:Number = -toDragLength*(num/_lenght);
			if(aix == "x"){
				TweenLite.to(_target,0.4,{x:num});
				TweenLite.to(_rollMc,0.4,{x:rollNum});
			}else{
				TweenLite.to(_target,0.4,{y:num});
				TweenLite.to(_rollMc,0.4,{y:rollNum});	
			}
			
			dragPage = getObjNum(-rollNum,allPageLenght,_totalPage+1);
				
			if(oldDragPage != dragPage){
				dispatchEvent(new Event("loadPage"));
				oldDragPage = dragPage;
			}
		}
		public function rollDragDot(n:Number):void{
			var cha:Number;
			if(aix == "x"){
				if(n != _totalPage){
					if(_totalPage == 1){
						_target.x = 0;
					}else{
						cha = ((_totalPage-1)*perPageLenght/toDragLength)*_lenght;
						_target.x = (cha/(_totalPage-1))*n;
					}
				}else{
					_target.x = _lenght;
				}
			}else{
				if(n != _totalPage){
					if(_totalPage == 1){
						_target.y = 0;
					}else{
						cha = ((_totalPage-1)*perPageLenght/toDragLength)*_lenght;
						_target.y = (cha/(_totalPage-1))*n;
					}
				}else{
					_target.y = _lenght;
				}
			}
			
		}
		
		
		private function getObjNum(aNum:Number,total:Number,off:Number):uint{
	        var objNum:uint = Math.min(off-1, Math.floor(aNum / total* off));
	        return objNum;
		}
		public function getDragPage():Number{
			return dragPage;
		}
		public function getPosition():Number{
			return num;
		}
		private function lastPageNum(n:Number):Number{
			var k:Number;
			if(numChild>pageNum){
				if(n == Math.ceil(numChild/pageNum)-1){
					k = numChild%pageNum;
					if(k ==0){
						k = pageNum;
					}	
				}else{
					k= pageNum;
				}
			}else{
				k =numChild;
			}
			return k;
		}
	}
}